﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Paypayes.Inventory
{
    public partial class ItemsListView : UserControl
    {
        private int sortColumn = -1;

        private String _filter;
        public String Filter { get { return _filter; } }

        public ItemsListView()
        {
            InitializeComponent();
        }

        public void GenerateList(String filter)
        {
            _filter = filter;
            listView1.Items.Clear();
            foreach (var item in MainForm.XElem.Elements())
            {
                if (item.Element("description").Value.Contains(filter))
                {
                    String[] subItem = new String[6];
                    subItem[0] = item.Attribute("id").Value;
                    subItem[1] = item.Element("description").Value;
                    if (item.Element("sizes") != null)
                    {
                        if (item.Element("sizes").Element("children") != null)
                        {
                            subItem[2] = item.Element("sizes").Element("children").Attribute("total").Value;
                        }
                        else
                        {
                            subItem[2] = "0";
                        }
                        if (item.Element("sizes").Element("adults") != null)
                        {
                            subItem[3] = item.Element("sizes").Element("adults").Attribute("total").Value;
                        }
                        else
                        {
                            subItem[3] = "0";
                        }
                    }
                    else
                    {
                        subItem[2] = "0";
                        subItem[3] = "0";
                    }
                    subItem[4] = item.Element("box").Value;
                    subItem[5] = item.Element("picture").Value;

                    listView1.Items.Add(new ListViewItem(subItem));
                }
            }
        }

        private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            e.DrawDefault = false;
        }

        private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawDefault = true;
        }

        private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            if (e.Header.Index == 5)
            {
                e.Graphics.DrawImage(MainForm.thumbnails[e.Item.Text], e.Bounds.X + 5, e.Bounds.Y + 5);
            }
            else
            {
                e.DrawDefault = true;
            }
        }

        public ListViewItem[] ef()
        {
            ListViewItem[] items = new ListViewItem[listView1.Items.Count];
            listView1.Items.CopyTo(items, 0);

            return items;
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listView1.SelectedItems != null)
            {
                AddChangeItemForm form = new AddChangeItemForm();
                form.Text = "Modifier un vêtement";
                form.Initialize(
                    MainForm.XElem.Elements("item")
                    .Single(x => (string)x.Attribute("id") == listView1.SelectedItems[0].Text));
                MainForm.IsModifying = true;
                form.ShowDialog(this);
                MainForm.IsModifying = false;
            }
        }

        private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            // Determine whether the column is the same as the last column clicked.
            if (e.Column != sortColumn)
            {
                // Set the sort column to the new column.
                sortColumn = e.Column;
                // Set the sort order to ascending by default.
                listView1.Sorting = SortOrder.Ascending;
            }
            else
            {
                // Determine what the last sort order was and change it.
                if (listView1.Sorting == SortOrder.Ascending)
                    listView1.Sorting = SortOrder.Descending;
                else
                    listView1.Sorting = SortOrder.Ascending;
            }

            // Call the sort method to manually sort.
            listView1.Sort();
            // Set the ListViewItemSorter property to a new ListViewItemComparer
            // object.
            this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column,
                                                              listView1.Sorting);
        }
    }

    class ListViewItemComparer : System.Collections.IComparer
    {
        private int col;
        private SortOrder order;
        public ListViewItemComparer()
        {
            col = 0;
            order = SortOrder.Ascending;
        }
        public ListViewItemComparer(int column, SortOrder order)
        {
            col = column;
            this.order = order;
        }
        public int Compare(object x, object y)
        {
            int returnVal = -1;
            if ((col == 2) || (col == 3))
            {
                returnVal = Convert.ToInt32(((ListViewItem)x).SubItems[col].Text).CompareTo(Convert.ToInt32(((ListViewItem)y).SubItems[col].Text));
            }
            else 
            {
                returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
                                        ((ListViewItem)y).SubItems[col].Text);
            }
            // Determine whether the sort order is descending.
            if (order == SortOrder.Descending)
                // Invert the value returned by String.Compare.
                returnVal *= -1;
            return returnVal;
        }
    }
}
